package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcConfigureOfflineAlertGroupMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.constant.AlertConfigConstant;
import com.csun.cmny.provider.model.domain.DmcConfigureOfflineAlertGroup;
import com.csun.cmny.provider.model.dto.alertConfig.AlertConfigQueryDto;
import com.csun.cmny.provider.model.vo.alertconfig.ConfigureOfflineAlertGroupVO;
import com.csun.cmny.provider.service.DmcConfigureOfflineAlertGroupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class
DmcConfigureOfflineAlertGroupServiceImpl extends BaseService<DmcConfigureOfflineAlertGroup> implements DmcConfigureOfflineAlertGroupService {

    @Resource
    private DmcConfigureOfflineAlertGroupMapper dmcConfigureOfflineAlertGroupMapper;

    @Override
    public Integer bind(Long configureAlertId, Long groupId) {

        DmcConfigureOfflineAlertGroup dmcConfigureOfflineAlertGroup = new DmcConfigureOfflineAlertGroup();
        dmcConfigureOfflineAlertGroup.setConfigureAlertId(configureAlertId);
        dmcConfigureOfflineAlertGroup.setGroupId(groupId);

        if(checkExsit(dmcConfigureOfflineAlertGroup)){
            throw new DmcBizException(ErrorCodeEnum.DMC100616001);
        }

        dmcConfigureOfflineAlertGroup.setStatus(AlertConfigConstant.ALERT_OFF);

        //清除系统缓存
        DmcDeviceAlertServiceImpl.groupOfflineAlertConfigMap.remove(groupId);

        return dmcConfigureOfflineAlertGroupMapper.insertSelective(dmcConfigureOfflineAlertGroup);
    }

    @Override
    public Integer changeStatus(Long configureAlertId, Long groupId) {
        DmcConfigureOfflineAlertGroup dmcConfigureOfflineAlertGroup = new DmcConfigureOfflineAlertGroup();
        dmcConfigureOfflineAlertGroup.setConfigureAlertId(configureAlertId);
        dmcConfigureOfflineAlertGroup.setGroupId(groupId);

        if(checkExsit(dmcConfigureOfflineAlertGroup)){
            DmcConfigureOfflineAlertGroup config = selectOne(dmcConfigureOfflineAlertGroup);

            switch (config.getStatus()){
                case AlertConfigConstant.ALERT_OFF:
                    config.setStatus(AlertConfigConstant.ALERT_ON);
                    break;
                case AlertConfigConstant.ALERT_ON:
                    config.setStatus(AlertConfigConstant.ALERT_OFF);
                    break;
                default:
                    throw new DmcBizException(ErrorCodeEnum.DMC100616003);
            }

            //清除系统缓存
            DmcDeviceAlertServiceImpl.groupOfflineAlertConfigMap.remove(groupId);

            return dmcConfigureOfflineAlertGroupMapper.updateByPrimaryKeySelective(config);
        }else{
            throw new DmcBizException(ErrorCodeEnum.DMC100616002);
        }
    }

    @Override
    public PageInfo<ConfigureOfflineAlertGroupVO> listWithPage(AlertConfigQueryDto alertConfigQueryDto) {

        PageHelper.startPage(alertConfigQueryDto.getPageNum(), alertConfigQueryDto.getPageSize());
        List<ConfigureOfflineAlertGroupVO> voList = dmcConfigureOfflineAlertGroupMapper.listWithPage(alertConfigQueryDto);
        return new PageInfo<>(voList);
    }

    @Override
    public List<ConfigureOfflineAlertGroupVO> list(AlertConfigQueryDto dto) {
        return dmcConfigureOfflineAlertGroupMapper.list(dto);
    }

    @Override
    public Integer unbind(Long configureAlertId, Long groupId) {
        DmcConfigureOfflineAlertGroup dmcConfigureOfflineAlertGroup = new DmcConfigureOfflineAlertGroup();
        dmcConfigureOfflineAlertGroup.setConfigureAlertId(configureAlertId);
        dmcConfigureOfflineAlertGroup.setGroupId(groupId);

        if(!checkExsit(dmcConfigureOfflineAlertGroup)){
            throw new DmcBizException(ErrorCodeEnum.DMC100616002);
        }

        //清除系统缓存
        DmcDeviceAlertServiceImpl.groupOfflineAlertConfigMap.remove(groupId);
        return dmcConfigureOfflineAlertGroupMapper.delete(dmcConfigureOfflineAlertGroup);
    }

    public boolean checkExsit(DmcConfigureOfflineAlertGroup dmcConfigureOfflineAlertGroup){
        List<DmcConfigureOfflineAlertGroup> list = dmcConfigureOfflineAlertGroupMapper.select(dmcConfigureOfflineAlertGroup);
        return list.size() > 0;
    }
}
